/*
 * @Author: your name
 * @Date: 2020-12-31 11:18:14
 * @LastEditTime: 2020-12-31 11:26:19
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \H5 2010 studyd:\H5 2010 练习\第三阶段 Vue + node\DAY 7\panda\src\store\plugins\cart.js
 */
export const cartPlugin = store => {
  // 为 store 添加监听，在每次 mutation 调用后执行回调函数
  store.subscribe((mutation, state) => {
    // console.log('plugins:', mutation, state)
    // 将购物车中的商品信息保存到本地存储中 (localStorage)
    if (mutation.type.startsWith('cart/')) {
      localStorage.setItem('cart', JSON.stringify(state.cart.cartItems))
    }
  })
}
